package com.runa.monitor.platinterface.extend.nb.utils;

import com.runa.monitor.platinterface.extend.nb.vo.temperature.ThermometerDataDTO;
import com.runa.protocol.util.UtilTime;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fuhui
 * @data 2021/10/8
 */
public class LKBMqttPanelUtils {

    private static final Logger logger = LoggerFactory.getLogger(LKBMqttPanelUtils.class);

    /**
     * 设备校时时间
     */
    public static void addCheckTimeCmd(ThermometerDataDTO thermometerDataDTO, JSONObject jsonObject) {
        long now = System.currentTimeMillis();
        if ("V1.0.0".equals(thermometerDataDTO.getSoft_version())) {
            now = (now / 1000) + (24 * 60 * 60);
        } else {
            now = now / 1000;
        }
        jsonObject.put("updatetime", now + "");
    }

    /**
     * 下发唤醒周期
     */
    public static void addWakeUpCycleCmd(JSONObject jsonObject, Boolean sendFlag, Integer wakeUpCycle, Date wakeUpCycleAddTime) {
        if (null != sendFlag && sendFlag) {
            if (null != wakeUpCycle && NewMqttSetUtils.checkCanSendWakeUpCycle(wakeUpCycleAddTime)) {
                jsonObject.put("up_data_interval", wakeUpCycle.toString());
                jsonObject.put("save_data_interval", wakeUpCycle.toString());
            }
        }
    }




    public static Map<Integer, Object> deviceDataDecode(String code) {
        Map<Integer, Object> map = new HashMap<>();
        map.put(24,new BigDecimal(Integer.parseInt(code.substring(36,38), 16)));//Signal
        map.put(25,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(38,42)), 16) * 0.01));//Voltage
        map.put(26,(UtilTime.StringToDate(packageTime(code), "yyyy-MM-dd HH:mm:ss")));
        map.put(0,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(58,62)), 16) * 0.1));
        map.put(1,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(64,68)), 16) * 0.1));
        map.put(2,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(70,74)), 16) * 0.1));
        map.put(3,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(76,80)), 16) * 0.1));
        map.put(4,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(82,86)), 16) * 0.1));
        map.put(5,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(88,92)), 16) * 0.1));
        map.put(6,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(94,98)), 16) * 0.1));
        map.put(7,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(100,104)), 16) * 0.1));
        map.put(8,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(106,110)), 16) * 0.1));
        map.put(9,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(112,116)), 16) * 0.1));
        map.put(10,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(118,122)), 16) * 0.1));
        map.put(11,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(124,128)), 16) * 0.1));
        map.put(12,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(130,134)), 16) * 0.1));
        map.put(13,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(136,140)), 16) * 0.1));
        map.put(14,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(142,146)), 16) * 0.1));
        map.put(15,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(148,152)), 16) * 0.1));
        map.put(16,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(154,158)), 16) * 0.1));
        map.put(17,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(160,164)), 16) * 0.1));
        map.put(18,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(166,170)), 16) * 0.1));
        map.put(19,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(172,176)), 16) * 0.1));
        map.put(20,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(178,182)), 16) * 0.1));
        map.put(21,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(184,188)), 16) * 0.1));
        map.put(22,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(190,194)), 16) * 0.1));
        map.put(23,new BigDecimal(Integer.parseInt(reverseHexString(code.substring(196,200)), 16) * 0.1));
        return map;
    }


    private static String packageTime(String code) {
        StringBuilder sb = new StringBuilder();
        int currentYear = LocalDate.now().getYear();
        String splitYear = String.valueOf(currentYear).substring(0,2);
        int year = Integer.parseInt(code.substring(46,48), 16);
        int month = Integer.parseInt(code.substring(48,50), 16);
        int day = Integer.parseInt(code.substring(50,52), 16);
        int hour = Integer.parseInt(code.substring(52,54), 16);
        int minute = Integer.parseInt(code.substring(54,56), 16);
        int second = Integer.parseInt(code.substring(56,58), 16);
        sb.append(splitYear).append(year).append("-").append(fillHex(month, 1)).append("-").append(fillHex(day, 1))
                .append(" ")
                .append(fillHex(hour, 1)).append(":").append(fillHex(minute, 1)).append(":").append(fillHex(second, 1));
        return sb.toString();
    }

    /**
     * 倒装
     * @param hexStr
     * @return
     */
    private static String reverseHexString(String hexStr) {
        StringBuilder res = new StringBuilder();
        for (int i = hexStr.length() / 2; i > 0; i--) {
            res.append(hexStr, 2 * i - 2, 2 * i);
        }
        return res.toString();
    }

    public static String fillHex(int hex, int byteLength) {
        StringBuilder hexString = new StringBuilder(String.valueOf(hex));
        int hexLength = hexString.length();
        if (hexLength < 2 * byteLength) {
            for (int i = 0; i < 2 * byteLength - hexLength; i++) {
                hexString.insert(0, "0");
            }
        }
        return hexString.toString();
    }
}
